/*获取pageId,versionNumber*/
var a = GetRequest();
var pageId = a['pageId'];
var versionNumber = a['versionNumber'];

/**
 * 页面加载执行科室查询
 * @type {string}
 */
var htmlOption = "";
$.ajax({
    url: "../../DropDownBoxQuery/findByhospitalId",
    type: "post",
    dataType: "json",
    success: function (map) {
        var departmentList = map.departmentList;
        for (var i = 0; i < departmentList.length; i++) {
            htmlOption += "<option value='" + departmentList[i].departmentId + "'>" + departmentList[i].departmentName + "</option>";
        }
    }
});

/**
 * 页面加载执行民族
 * @type {string}
 */
var htmlNationalOption = "";
$.ajax({
    url: "../../DropDownBoxQuery/findNational",
    type: "post",
    dataType: "json",
    success: function (map) {
        var nationalList = map.nationalList;
        for (var i = 0; i < nationalList.length; i++) {
            htmlNationalOption += "<option value='" + nationalList[i].nationalId + "'>" + nationalList[i].nationalName + "</option>"
        }
    }
});

/**
 * 页面右面展示
 */
$.ajax({
    url: "../../template/selectTemplatePageIdVersionNumber",
    type: "post",
    dataType: "json",
    data: {
        pageId: pageId,
        versionNumber: versionNumber
    },
    success: function (map) {
        var templatesHtml = "";
        var templates = map.templates;
        for (var i = 0; i < templates.length; i++) {
            /**拼接页面右边数据展示*/
            /*向页面上拼接数据*/
            if (templates[i].title != "无") {
                templatesHtml += "<div class='col-sm-12' style='height: 80px; line-height: 80px;'>";
                templatesHtml += "<p style='font-size: 20px;'><b>" + templates[i].title + "</b></p>";
                templatesHtml += "</div>";
            } else {//剩下每一个模板除去第一个数据
                //实现吧数据库字段改成实体类字段
                var ziduanming = templates[i].fieldName.substring(0, templates[i].fieldName.indexOf("-"));
                if (ziduanming.indexOf('_') == -1) {
                    ziduanming = ziduanming
                } else {
                    for (var k = 0; k < 10; k++) {
                        var index = ziduanming.indexOf('_');//获取第一个下划线位置
                        if (index == -1) {
                            break;
                        } else {
                            var number = ziduanming.charAt(index + 1);//获取下划线后面的字母
                            var number = number.toUpperCase();//把下划线后面的字母改为大写
                            var tihuan = ziduanming.substring(index, index + 2);
                            ziduanming = ziduanming.replace(tihuan, number);
                        }
                    }
                }
                //消除模板数据表中的最后一个空数据
                if (templates[i].fieldName.indexOf("-") != 0) {
                    if (templates[i].typeOfFrame == "select") {
                    } else {
                        templatesHtml += "<div class='form-group col-sm-6' style='font-size: 16px;'>";
                        templatesHtml += "<label class='col-sm-3 control-label'>" + templates[i].fieldName.substring(templates[i].fieldName.indexOf("-") + 1) + "</label>";
                        templatesHtml += "<div class='col-sm-5'>";
                    }
                    if (templates[i].typeOfFrame == "select") {//去掉下拉框数据展示
                        if (templates[i].fieldName == "ward_id-病区") {
                            templatesHtml += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                            templatesHtml += "<label class='col-sm-1 control-label'>" + templates[i].fieldName.substring(templates[i].fieldName.indexOf("-") + 1) + "</label>";
                            templatesHtml += "<div class='col-sm-11'>";
                            /*展示病区（病区楼、楼层、房间、床位）信息*/
                            templatesHtml += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                            templatesHtml += "<div class='col-sm-12'>";
                            templatesHtml += "<div class='btn-toolbar' role='toolbar'>";
                            templatesHtml += "<div class='btn-group btn-group-xs'>";
                            templatesHtml += "<select class='btn btn-default' name='wardBuilding' id='buildingList'></select>";
                            templatesHtml += "<input class='btn btn-default' value='楼' readonly style='width: 50px;' />";
                            templatesHtml += "<select class='btn btn-default' name='wardLayer' id='layerList'></select>";
                            templatesHtml += "<input class='btn btn-default' value='楼层' readonly style='width: 50px;' />";
                            templatesHtml += "<select class='btn btn-default' name='wardRoom' id='roomList'></select>";
                            templatesHtml += "<input class='btn btn-default' value='房间' readonly style='width: 50px;' />";
                            templatesHtml += "<select class='btn btn-default' name='wardBed' id='bedList'></select>";
                            templatesHtml += "<input class='btn btn-default' value='床位' readonly style='width: 50px;' />";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                        } else if (templates[i].fieldName.indexOf("place_of_birth_id") != -1) {
                            templatesHtml += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                            templatesHtml += "<label class='col-sm-1 control-label'>" + templates[i].fieldName.substring(templates[i].fieldName.indexOf("-") + 1) + "</label>";
                            templatesHtml += "<div class='col-sm-11'>";
                            /*展示省市县信息*/
                            templatesHtml += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                            templatesHtml += "<div class='col-sm-12'>";
                            templatesHtml += "<div class='btn-toolbar' role='toolbar'>";
                            templatesHtml += "<div class='btn-group btn-group-xs'>";
                            templatesHtml += "<select class='btn btn-default' name='provinceCode1' id='province'></select>";
                            templatesHtml += "<input class='btn btn-default' value='省' readonly style='width: 25px;' />";
                            templatesHtml += "<select class='btn btn-default' name='cityCode1' id='city'></select>";
                            templatesHtml += "<input class='btn btn-default' value='市' readonly style='width: 25px;' />";
                            templatesHtml += "<select class='btn btn-default' name='countyCode1' id='county'></select>";
                            templatesHtml += "<input class='btn btn-default' value='县' readonly style='width: 25px;' />";
                            templatesHtml += "<input class='btn btn-default' name='gates1' placeholder='如：新宫家园北区7号楼1单元504室'/>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                        } else if (templates[i].fieldName.indexOf("address_id") != -1) {
                            templatesHtml += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                            templatesHtml += "<label class='col-sm-1 control-label'>" + templates[i].fieldName.substring(templates[i].fieldName.indexOf("-") + 1) + "</label>";
                            templatesHtml += "<div class='col-sm-11'>";
                            templatesHtml += "<div class='form-group col-sm-12' style='font-size: 16px;'>";
                            templatesHtml += "<div class='col-sm-12'>";
                            templatesHtml += "<div class='btn-toolbar' role='toolbar'>";
                            templatesHtml += "<div class='btn-group btn-group-xs'>";
                            templatesHtml += "<select class='btn btn-default' name='provinceCode2' id='province1'></select>";
                            templatesHtml += "<input class='btn btn-default' value='省' readonly style='width: 25px;' />";
                            templatesHtml += "<select class='btn btn-default' name='cityCode2' id='city1'></select>";
                            templatesHtml += "<input class='btn btn-default' value='市' readonly style='width: 25px;' />";
                            templatesHtml += "<select class='btn btn-default' name='countyCode2' id='county1'></select>";
                            templatesHtml += "<input class='btn btn-default' value='县' readonly style='width: 25px;' />";
                            templatesHtml += "<input class='btn btn-default' name='gates2' placeholder='如：新宫家园北区7号楼1单元504室'/>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                            templatesHtml += "</div>";
                        } else if (templates[i].fieldName.indexOf("department") != -1 || templates[i].fieldName.indexOf("departments") != -1) {
                            templatesHtml += "<div class='form-group col-sm-6' style='font-size: 16px;'>";
                            templatesHtml += "<label class='col-sm-3 control-label'>" + templates[i].fieldName.substring(templates[i].fieldName.indexOf("-") + 1) + "</label>";
                            templatesHtml += "<div class='col-sm-5'>";
                            templatesHtml += "<select name='" + ziduanming + "' class='form-control'>" + htmlOption + "</select>";
                        } else if (templates[i].fieldName.indexOf("national") != -1) {
                            templatesHtml += "<div class='form-group col-sm-6' style='font-size: 16px;'>";
                            templatesHtml += "<label class='col-sm-3 control-label'>" + templates[i].fieldName.substring(templates[i].fieldName.indexOf("-") + 1) + "</label>";
                            templatesHtml += "<div class='col-sm-5'>";
                            templatesHtml += "<select name=" + ziduanming + " class='form-control'>" + htmlNationalOption + "</select>";
                        }
                    } else {
                        if (templates[i].fieldName.indexOf("date") != -1 || templates[i].fieldName.indexOf("days") != -1 || templates[i].fieldName.indexOf("time") != -1) {
                            templatesHtml += "<input type='date' name='" + ziduanming + "Str' class='form-control' />";
                        } else if (templates[i].fieldName.indexOf("gender") != -1) {
                            templatesHtml += "<select name='" + ziduanming + "' class='form-control'>"
                            templatesHtml += "<option value='0'>请选择</option>"
                            templatesHtml += "<option value='1'>男</option>"
                            templatesHtml += "<option value='2'>女</option>"
                            templatesHtml += "</select>"
                        } else if (templates[i].fieldName.indexOf("fo_group_state") != -1) {
                            templatesHtml += "<select name='" + ziduanming + "' class='form-control'>"
                            templatesHtml += "<option value='0'>请选择</option>"
                            templatesHtml += "<option value='1'>是</option>"
                            templatesHtml += "<option value='2'>否</option>"
                            templatesHtml += "</select>"
                        } else if (templates[i].fieldName.indexOf("marriage") != -1) {
                            templatesHtml += "<select name='" + ziduanming + "' class='form-control'>"
                            templatesHtml += "<option value='0'>请选择</option>"
                            templatesHtml += "<option value='1'>已婚</option>"
                            templatesHtml += "<option value='2'>未婚</option>"
                            templatesHtml += "<option value='2'>离异</option>"
                            templatesHtml += "</select>"
                        } else if (templates[i].fieldName.indexOf("clinical_research") != -1) {
                            templatesHtml += "<select name='" + ziduanming + "' class='form-control'>"
                            templatesHtml += "<option value='0'>请选择</option>"
                            templatesHtml += "<option value='1'>是</option>"
                            templatesHtml += "<option value='2'>否</option>"
                            templatesHtml += "</select>"
                        } else {
                            templatesHtml += "<input  name='" + ziduanming + "' class='form-control' />";
                        }
                    }
                }
            }
            templatesHtml += "</div>";
            templatesHtml += "</div>";
        }
        templatesHtml += "<div class='col-sm-12' style='margin: 20px 0 50px 0;'>"
        templatesHtml += "<div class='col-sm-3 col-sm-offset-4'>"
        templatesHtml += "<button class='btn btn-primary' id='btn_save'>保存</button>"
        templatesHtml += "</div>"
        templatesHtml += "<div class='col-sm-3'>"
        templatesHtml += "<a href='../../patientSignsAnemia/selectTemplatemDepartment'>"
        templatesHtml += "<button type='button' class='btn btn-primary'>返回</button>"
        templatesHtml += "</a>"
        templatesHtml += "</div>"
        templatesHtml += "</div>"
        $("#show_template").html(templatesHtml);
    }
})

/**
 * 点击保存事件
 */
$("#btn_save").click(function () {
    console.log("保存按钮执行");
    $("#show_template").submit();
})

/**
 * 通过路径获取id
 * @returns
 */
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

$(function () {

    /**
     * 页面加载执行省市县
     * @type {string}
     */
    $.ajax({
        url: "../../DropDownBoxQuery/findAddress",
        type: "post",
        dataType: "json",
        success: function (map) {
            //console.log(map);
            var citylist = map.cityList;
            var countylist = map.countyList;
            var provincelist = map.provinceList;
            var province_select = document.getElementById("province");
            var city_select = document.getElementById("city");
            var county_select = document.getElementById("county");
            //消除下拉框
            province_select.innerHTML = "<option value='0'>选择</option>";
            city_select.innerHTML = "<option value='0'>选择</option>";
            county_select.innerHTML = "<option value='0'>选择（区）</option>"
            //加载省份
            for (var i = 0; i < provincelist.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = provincelist[i].name;
                opt.setAttribute("value", provincelist[i].code);
                province_select.appendChild(opt);
            }
            //给省份添加change事件，选择省份，获取城市
            province_select.onchange = function () {
                //获取省份ID
                var provinceCode = province_select.value;
                //先清除原来的城市数据
                city_select.innerHTML = "<option value='0'>选择</option>";
                county_select.innerHTML = "<option value='0'>选择区</option>";
                //根据省份code获取 城市列表(遍历省份去匹配)
                for (var i = 0; i < citylist.length; i++) {
                    if (citylist[i].provincecode == provinceCode) {
                        var citys = citylist[i].name;
                        var opt = document.createElement("option");
                        opt.innerText = citylist[i].name;
                        opt.setAttribute("value", citylist[i].code);
                        city_select.appendChild(opt);
                    }
                }
            }
            //给城市添加change事件，选择城市，获取区县
            city_select.onchange = function () {
                //获取城市ID
                var cityCode = city_select.value;
                //先清除原来的城市数据
                county_select.innerHTML = "<option value='0'>选择区</option>";
                //根据城市ID获取区县列表
                for (var i = 0; i < countylist.length; i++) {
                    if (countylist[i].citycode == cityCode) {
                        var opt = document.createElement("option");
                        opt.innerText = countylist[i].name;
                        opt.setAttribute("value", countylist[i].code);
                        county_select.appendChild(opt);
                    }
                }
            }
            var province_select1 = document.getElementById("province1");
            var city_select1 = document.getElementById("city1");
            var county_select1 = document.getElementById("county1");
            //消除下拉框
            province_select1.innerHTML = "<option value='0'>选择</option>";
            city_select1.innerHTML = "<option value='0'>选择</option>";
            county_select1.innerHTML = "<option value='0'>选择（区）</option>"
            //加载省份
            for (var i = 0; i < provincelist.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = provincelist[i].name;
                opt.setAttribute("value", provincelist[i].code);
                province_select1.appendChild(opt);
            }
            //给省份添加change事件，选择省份，获取城市
            province_select1.onchange = function () {
                //获取省份ID
                var provinceCode = province_select1.value;
                //先清除原来的城市数据
                city_select1.innerHTML = "<option value='0'>选择</option>";
                county_select1.innerHTML = "<option value='0'>选择区</option>";
                //根据省份code获取 城市列表(遍历省份去匹配)
                for (var i = 0; i < citylist.length; i++) {
                    if (citylist[i].provincecode == provinceCode) {
                        var opt = document.createElement("option");
                        opt.innerText = citylist[i].name;
                        opt.setAttribute("value", citylist[i].code);
                        city_select1.appendChild(opt);
                    }
                }
            }
            //给城市添加change事件，选择城市，获取区县
            city_select1.onchange = function () {
                //获取城市ID
                var cityCode = city_select1.value;
                //先清除原来的城市数据
                county_select1.innerHTML = "<option value='0'>选择区</option>";
                //根据城市ID获取区县列表
                for (var i = 0; i < countylist.length; i++) {
                    if (countylist[i].citycode == cityCode) {
                        var opt = document.createElement("option");
                        opt.innerText = countylist[i].name;
                        opt.setAttribute("value", countylist[i].code);
                        county_select1.appendChild(opt);
                    }
                }
            }
        }
    });

    /**
     *  病区楼、楼层、房间、床位
     */
    $.ajax({
        url: "../../DropDownBoxQuery/findWard",
        type: "post",
        dataType: "json",
        success: function (map) {
            var buildingList = map.buildingList;
            var layerList = map.layerList;
            var roomList = map.roomList;
            var bedList = map.bedList;
            var buildingList_select = document.getElementById("buildingList");
            var layerList_select = document.getElementById("layerList");
            var roomList_select = document.getElementById("roomList");
            var bedList_select = document.getElementById("bedList");
            //消除下拉框
            buildingList_select.innerHTML = "<option value='0'>选择病区楼</option>"
            layerList_select.innerHTML = "<option value='0'>选择楼层</option>"
            roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
            bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
            //加载病区楼
            for (var i = 0; i < buildingList.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = buildingList[i].wardBuildingName;
                opt.setAttribute("value", buildingList[i].wardBuildingCode);
                buildingList_select.appendChild(opt);
            }
            //给病区楼添加change事件，选择病区楼，获取楼层
            buildingList_select.onchange = function () {
                //获取病区楼ID
                var buildingLisCode = buildingList_select.value;
                //先清除原来的病区楼数据
                layerList_select.innerHTML = "<option value='0'>选择楼层</option>"
                roomList_select.innerHTML = "<option value='0'>选择病房号</option>";
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据病区楼code获取 楼层列表(遍历病区楼去匹配)
                for (var i = 0; i < layerList.length; i++) {
                    if (layerList[i].wardBuildingCode == buildingLisCode) {
                        var opt = document.createElement("option");
                        opt.innerText = layerList[i].wardLayerName;
                        opt.setAttribute("value", layerList[i].wardLayerCode);
                        layerList_select.appendChild(opt);
                    }
                }
            }
            //给楼层添加change事件，选择楼层，获取病区房号
            layerList_select.onchange = function () {
                //获取楼层ID
                var layerCode = layerList_select.value;
                //先清除原来的楼层数据
                roomList_select.innerHTML = "<option value='0'>选择病房号</option>";
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据区房号ID获取区县列表
                for (var i = 0; i < roomList.length; i++) {
                    if (roomList[i].wardLayerCode == layerCode) {
                        var opt = document.createElement("option");
                        opt.innerText = roomList[i].wardRoomName;
                        opt.setAttribute("value", roomList[i].wardRoomCode);
                        roomList_select.appendChild(opt);
                    }
                }
            }
            //给病区房号添加change事件，选择病区房号，获取床位号
            roomList_select.onchange = function () {
                //获取病区房号ID
                var roomCode = roomList_select.value;
                //先清除原来的城市数据
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据病区房号ID获取区县列表
                for (var i = 0; i < bedList.length; i++) {
                    if (bedList[i].wardRoomCode == roomCode) {
                        var opt = document.createElement("option");
                        opt.innerText = bedList[i].wardBedName;
                        opt.setAttribute("value", bedList[i].wardBedCode);
                        bedList_select.appendChild(opt);
                    }
                }
            }
        }
    });

})